//
//  FlexElement.swift
//  flow
//
//  Created by tong on 2017/7/14.
//  Copyright © 2017年 tong. All rights reserved.
//

import UIKit
import AsyncDisplayKit


private struct FlowAssociatedKeys {
    static var sid = "specId_key"
}

extension ASLayoutElement{
    
    public var sid:String?{
        get {
            if let obj = objc_getAssociatedObject(self, &FlowAssociatedKeys.sid) as? String
            {
                return obj
            }
            return nil
        }
        set {
            objc_setAssociatedObject(self, &FlowAssociatedKeys.sid, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public func subSpec(specId:String) ->ASLayoutElement? {
        if let s_id = self.sid
        {
            if s_id == specId
            {
                return self

            }
        }
        
        
        var child:ASLayoutElement?
        if let nchild = (self as? ASInsetLayoutSpec)?.child
        {
            child = nchild
        }
        if let nchild = (self as? ASOverlayLayoutSpec)?.child
        {
            child = nchild
        }
        if let nchild = (self as? ASBackgroundLayoutSpec)?.child
        {
            child = nchild
        }
        if let nchild = (self as? ASCenterLayoutSpec)?.child
        {
            child = nchild
        }
        
        if let validChild = child
        {
            return validChild.subSpec(specId: specId)
        }
        
        
        if let childs = (self as? ASStackLayoutSpec)?.children
        {
            for child in childs
            {
                if let one = child.subSpec(specId: specId)
                {
                    return one
                }
            }
            
        }
        
        return nil
    }

}


